<?php
function func_response($str = '')
{
    echo $str;exit;
}
function func_get($str){
    return isset($_GET[$str]) ? $_GET[$str] : '';
}
function func_request($str){
    return isset($_REQUEST[$str]) ? $_REQUEST[$str] : '';
}
function func_post($str){
    return isset($_POST[$str]) ? $_POST[$str] : '';
}
require('./db.php');
session_start();
$u_id = $_SESSION['u_id'];
?>
<meta charset="utf-8" >
<meta http-equiv="content-type" content="text/html" >
<META HTTP-EQUIV="Pragma"   CONTENT="no-cache">
<meta name="viewport" content="initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no,width=device-width"   />
<meta name="format-detection" content="telephone=no" />
<meta name="app-mobile-web-app-capable"  content="yes" />
<meta name="app-mobile-web-app-status-bar-style" content="black-translucent" />
<script>
    function postInfos(URL,postStr,functions,s){
        var xmlHttp=xmlInit();
        xmlHttp.open("POST", URL, true);
        xmlHttp.setRequestHeader("X-Requested-With","XMLHttpRequest");
        xmlHttp.setRequestHeader("Content-TYPE","application/x-www-form-urlencoded");
        xmlHttp.send(postStr);
        xmlHttp.onreadystatechange = function(){
            if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
                var pReturn="";
                pReturn=xmlHttp.responseText;
                functions(pReturn,s);
            }
        }

    }
    function xmlInit(){
        var xmlHttp = false;
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlHttp =  new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e2) {
                xmlHttp =  false;
            }
        }
        if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
            xmlHttp =  new XMLHttpRequest();
        }
        return xmlHttp;
    }
</script>
<?php if(!$u_id) { ?>
<div id="login-box">
    <input id="t"  type="hidden" name="t" value='login' />
    <p>账号: <input id="u_name" type="text" name="u_name" /></p>
    <p>密码: <input id="u_pwd" type="text" name="u_pwd" /></p>
    <input type="button" value="登录" onclick="submit()"/>
</div>
<script>
    function submit(){
        var t = document.getElementById("t");
        var u_name = document.getElementById("u_name");
        var u_pwd = document.getElementById("u_pwd");
        if(u_name.value === '') {
            alert('账号不能为空');
            return false;
        }
        if(u_pwd.value === '') {
            alert('密码不能为空');
            return false;
        }
        var url = "api.php";
        var params= "t=" + t.value + "&u_name=" + u_name.value + "&u_pwd=" + u_pwd.value
        postInfos(url,params,function (res) {
            if(res === '登录成功') {
                alert(res);
                window.location.reload();
                return false;
            }
            alert(res);
        })
        return false;
    }
</script>
<?php } else { ?>
<?php
    $sql = "select id,is_admin from lyl_users where id=" . $u_id;
    $user = Database::query($sql);
    if ($user) {
        $is_admin = 0;
        if($user[0]['is_admin']) {
            $is_admin = 1;
        }
    } else {
        $_SESSION['u_id'] = null;
        header("Location: /");
        exit;
    }
    if(!$is_admin) {
        header("Location: /sing.php");exit;
    }


?>
<script>
    function out() {
        var url = "api.php";
        var params= "t=out";
        postInfos(url,params,function (res) {
            alert(res);
            window.location.reload();
        })
    }
</script>
<?php if($is_admin) { ?>
<a href="/">已填入列表</a>
<a href="/src/user.php.php">人员列表</a>
        <?php } ?>
<a href="/src/sing.php.php">信息列表</a>
<a href="javascript:;" onclick="out()">退出</a>

<style>
    #hor-zebra
    {
        font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
        font-size: 12px;
        width: 100%;
        text-align: left;
        border-collapse: collapse;
    }
    #hor-zebra th
    {
        font-size: 14px;
        font-weight: normal;
        padding: 10px 8px;
        color: #039;
    }
    #hor-zebra td
    {
        padding: 8px;
        color: #669;
    }
    #hor-zebra .odd
    {
        background: #e8edff;
    }
</style>
<h2 style="text-align: center">已填入列表</h2>
    <div style="text-align: center;">
	    <?php
        $use = func_request("use");
        if(!in_array($use,[1,2,3])) {
            $use = 1;
        }
        $use_arr = [1 => '全部', 2 => '隐藏', 3 => '显示'];

	    $f_name = func_request("f_name");
	    if(!is_numeric($f_name) || $f_name < 0) {
		    $f_name = '0';
	    }
	    $sql = "select id,f_name from lyl_users";
	    $f_name_rs = Database::query($sql);
	    if (!$f_name_rs) {
		    $f_name_rs = [];
	    }
	    $f_name_arr = [];
	    foreach ($f_name_rs as $item) {
		    $f_name_arr[$item['id']] = $item['f_name'];
	    }


	    $type = func_request("type");
	    $sql = "select type from lyl_singleton group by `type`";
	    $type_rs = Database::query($sql);
	    if (!$type_rs) {
		    $type_rs = [];
	    }
	    $type_arr = [];
	    foreach ($type_rs as $item) {
		    $type_arr[] = $item['type'];
	    }

	    $code = func_request("code");
	    $sql = "select code from lyl_singleton group by `code`";
	    $code_rs = Database::query($sql);
	    if (!$code_rs) {
		    $code_rs = [];
	    }
	    $code_arr = [];
	    foreach ($code_rs as $item) {
		    $code_arr[] = $item['code'];
	    }

	    $name = func_request("name");

	    ?>
        <?php
        $p = func_request("p");
        $size = 20;
        if(!$p) {
	        $p = 1;
        }
        $sql = "select count(id) c from lyl_bill";
        $count_rs = Database::query($sql);
        if (!$count_rs) {
	        $c = 0;
        } else {
	        $c = $count_rs[0]['c'];
        }
        $whereSql = ' where 1 = 1';
        if($f_name > 0) {
	        $whereSql .= " and a.u_id={$f_name}";
        }
        if(!empty($type)) {
	        $whereSql .= " and c.type='{$type}'";
        }
        if(!empty($code)) {
	        $whereSql .= " and c.code='{$code}'";
        }
        if(!empty($name)) {
	        $whereSql .= " and c.name like '%{$name}%'";
        }

        if(in_array($use,[2,3])) {
            $whereSql .= " and c.use=" . ($use - 2);
        }

        require('./page.php');
        $page = new Page(10,$c,3);
        $sql = "SELECT a.id,a.num ing,b.f_name,c.name,c.code,c.type,c.use,c.num,c.n,c.price FROM lyl_bill a INNER JOIN lyl_users b ON a.u_id = b.id INNER JOIN lyl_singleton c ON a.s_id = c.id ". $whereSql ." limit " .$page->offestPage.','.$page->colNum;
        $list = Database::query($sql);
        if (!$list) {
	        $list = [];
        }

        ?>
        <form name="form1" method="post" action="?">
            显示筛选：
            <select name='use'>
                <?php foreach($use_arr as $k => $item) { ?>
                    <option value="<?=$k?>" <?=($use == $k ? 'selected' : '')?>><?=$item?></option>
                <?php } ?>
            </select>
            &nbsp;

            名称：
            <input type="text" name="name" value="<?=$name ?>">
            &nbsp;<br>

            谁填入：
            <select name='f_name'>
                <option value="0" <?=($f_name == '0' ? 'selected' : '')?>>全部</option>
                <?php foreach($f_name_arr as $k => $item) { ?>
                    <option value="<?=$k?>" <?=($f_name == $k ? 'selected' : '')?>><?=$item?></option>
                <?php } ?>
            </select>
            &nbsp;
            &nbsp;
            类型：
            <select name='type'>
                <option value="" <?=($type == '' ? 'selected' : '')?>>全部</option>
		        <?php foreach($type_arr as $item) { ?>
                    <option value="<?=$item?>" <?=($type == $item ? 'selected' : '')?>><?=$item?></option>
		        <?php } ?>
            </select>
            &nbsp;
            <br>
            &nbsp;
            编号：
            <select name='code'>
                <option value="" <?=($code == '' ? 'selected' : '')?>>全部</option>
		        <?php foreach($code_arr as $item) { ?>
                    <option value="<?=$item?>" <?=($code == $item ? 'selected' : '')?>><?=$item?></option>
		        <?php } ?>
            </select>
            <br>
            <input type="submit" name="Submit2" style="background-color:#FFCC00;  border-color:#996600;width:80px; height:25px;margin-top: 5px;" value="搜 索">
        </form>
    </div>
    <div style="text-align: center;">
		<?=$page->show();?>
    </div>
<table id="hor-zebra" summary="Employee Pay Sheet">
    <thead>
    <tr>
        <th scope="col">id</th>
        <th scope="col">名称</th>
        <th scope="col">编号</th>
        <th scope="col">类型</th>
        <th scope="col">状态</th>
        <th scope="col">总数</th>
        <th scope="col">剩余</th>
        <th scope="col">单价</th>
        <th scope="col">谁填入</th>
        <th scope="col">已填数</th>
    </tr>
    </thead>
    <tbody>
    <?php foreach ($list as $item) { ?>
        <tr class="odd">
            <td><?=$item["id"] ?> </td>
            <td><?=$item["name"] ?></td>
            <td><?=$item["code"] ?></td>
            <td><?=$item["type"] ?></td>
            <td><?=$item["use"] == 1 ? '显示' : '隐藏'?></td>
            <td><?=$item["num"] ?></td>
            <td><?=($item["num"] - $item["n"])  ?></td>
            <td><?=$item["price"] ?></td>
            <td><?=$item["f_name"] ?></td>
            <td><?=$item["ing"] ?></td>
        </tr>
    <?php } ?>
    </tbody>
</table>
<?php }?>
